package cl.clientews.rest;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpDelete;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpPut;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;

import cl.clienterest.conexion.ConstantesConexion;
import cl.clienterest.util.Util;

public class PersonasSOAWS {

	/**
	 * 
	 * @param rut
	 * @param verificador
	 * @param nombre
	 * @param params
	 * @return
	 */
	public String addPersona(String rut, String verificador, String nombre, String params) {
		DefaultHttpClient httpclient = new DefaultHttpClient();
		try {
			HttpPost httpPost = new HttpPost(ConstantesConexion.url_base + "/persona/add");

			System.out.println(httpPost.getURI());

			List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
			nameValuePairs.add(new BasicNameValuePair("rut", rut));
			nameValuePairs.add(new BasicNameValuePair("verificador", verificador));
			nameValuePairs.add(new BasicNameValuePair("nombre", nombre));
			nameValuePairs.add(new BasicNameValuePair("sexo", params + ""));
			httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

			// StringEntity input = new StringEntity(json);
			// input.setContentType("application/json");
			// httpPost.setEntity(input);

			HttpResponse responseRequest = httpclient.execute(httpPost);
			int rc = responseRequest.getStatusLine().getStatusCode();
			if ((rc != 204) && (rc != 201) && (rc != 200)) {
				throw new RuntimeException("Failed : HTTP error code : "
						+ responseRequest.getStatusLine().getStatusCode());
			}

			String resp = Util.getStringFromInputStream(responseRequest.getEntity().getContent());

			return resp;
		} catch (IllegalStateException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			httpclient.getConnectionManager().shutdown();
		}
		return "null";

	}

	/**
	 * 
	 * @param rut
	 * @return
	 */
	public String deletePersona(String rut) {

		DefaultHttpClient httpclient = new DefaultHttpClient();
		try {
			HttpDelete httpDelete = new HttpDelete(ConstantesConexion.url_base + "/persona/delete/" + rut);

			System.out.println(httpDelete.getURI());

			HttpResponse responseRequest = httpclient.execute(httpDelete);
			int rc = responseRequest.getStatusLine().getStatusCode();
			if ((rc != 204) && (rc != 201) && (rc != 200)) {
				throw new RuntimeException("Failed : HTTP error code : "
						+ responseRequest.getStatusLine().getStatusCode());
			}

			String resp = Util.getStringFromInputStream(responseRequest.getEntity().getContent());

			return resp;
		} catch (IllegalStateException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			httpclient.getConnectionManager().shutdown();
		}
		return null;
	}

	/**
	 * 
	 * @param nombre_o_rut
	 * @param campo, 0 para cambiar el nombre, 1 para cambiar el rut
	 * @param new_nom
	 * @return
	 */
	public String updatePersona(String nombre_o_rut, String campo, String new_nom) {

		DefaultHttpClient httpclient = new DefaultHttpClient();
		try {
			HttpPut httpPost = new HttpPut(ConstantesConexion.url_base + "/persona/update");

			System.out.println(httpPost.getURI());

			List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
			nameValuePairs.add(new BasicNameValuePair("nombre_o_rut", nombre_o_rut));
			nameValuePairs.add(new BasicNameValuePair("campo", campo));
			nameValuePairs.add(new BasicNameValuePair("nuevo_nombre", new_nom));
			httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

			HttpResponse responseRequest = httpclient.execute(httpPost);
			int rc = responseRequest.getStatusLine().getStatusCode();
			if ((rc != 204) && (rc != 201) && (rc != 200)) {
				throw new RuntimeException("Failed : HTTP error code : "
						+ responseRequest.getStatusLine().getStatusCode());
			}

			String resp = Util.getStringFromInputStream(responseRequest.getEntity().getContent());

			return resp;
		} catch (IllegalStateException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			httpclient.getConnectionManager().shutdown();
		}
		return null;

	}

	/**
	 * 
	 * @param buscar
	 * @return
	 */
	public String findPersona(String buscar) {

		DefaultHttpClient httpclient = new DefaultHttpClient();
		try {
			HttpGet httpGet = new HttpGet(ConstantesConexion.url_base + "/persona/find/" + buscar);
			System.out.println(httpGet.getURI());

			HttpResponse responseRequest = httpclient.execute(httpGet);
			int rc = responseRequest.getStatusLine().getStatusCode();
			if ((rc != 204) && (rc != 201) && (rc != 200)) {
				throw new RuntimeException("Failed : HTTP error code : "
						+ responseRequest.getStatusLine().getStatusCode());
			}

			String resp = Util.getStringFromInputStream(responseRequest.getEntity().getContent());

			return resp;
		} catch (IllegalStateException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			httpclient.getConnectionManager().shutdown();
		}
		return null;

	}

	/**
	 * 
	 * @param buscar
	 * @return
	 */
	public String findAllPersona(String buscar) {

		DefaultHttpClient httpclient = new DefaultHttpClient();
		try {
			HttpGet httpGet = new HttpGet(ConstantesConexion.url_base + "/persona/findall/" + buscar);
			System.out.println(httpGet.getURI());

			HttpResponse responseRequest = httpclient.execute(httpGet);
			int rc = responseRequest.getStatusLine().getStatusCode();
			if ((rc != 204) && (rc != 201) && (rc != 200)) {
				throw new RuntimeException("Failed : HTTP error code : "
						+ responseRequest.getStatusLine().getStatusCode());
			}

			String resp = Util.getStringFromInputStream(responseRequest.getEntity().getContent());

			return resp;
		} catch (IllegalStateException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			httpclient.getConnectionManager().shutdown();
		}
		return null;

	}

}
